
package models.spider.sink;

import java.util.ArrayList;
import java.util.List;

import models.Store;
import models.aaa.Asset;
import models.aaa.Resource;
import models.spider.ISpider;
import models.spider.parser.IParser;
import models.spider.request.Request;

import org.apache.commons.lang.StringUtils;

/**
 * 
 * @author laurent@opprecht.info
 */
public class AAA extends SinkBase {

    protected IParser parser = null;

    public AAA(final IParser parser) {
        this.parser = parser;
    }

    public AAA() {
        IParser parser = new models.spider.parser.aaa.Parser();
        this.parser = parser;
    }

    @Override
    public void sink(final Request request, final ISpider spider) {
        List<String> result = new ArrayList<>();
        List<Asset> items = parser.parse(request, spider);

        for (Asset item : items) {
            Store.save(item);
            // Long id = item.getGroup().getId();
            for (Asset to : item.getLinks()) {
                for (Resource res : to.getResources()) {
                    String url = res.getUrl().toString();
                    if (!StringUtils.isEmpty(url)) {
                        result.add(url);
                    }
                }
            }
        }
    }

}
